-- CALC_USER_MAGIC
-----------------------------------------------
--  玩家魔力
-- @param user    玩家对象
-- @param args    是否包含削弱属性
return function(user, args)
    -- 原始魔力
    local magic = user.dbase:query("magic", 0);
    local addon = 0;

    local combine = PropM.combine;
    local apply = PropM.apply;
    local fetchProps = PropM.fetchProps;
    local trigger = PropM.trigger;

    -- --------------- 基础部分 ------------------------------------------
    -- --------------- 主要包括天赋的提升，和装备、技能的绝对值提升 ---------
    -- 绝对值加成
    local prop = combine(user, "attrib2", "magic");
    magic = apply(prop, magic);

    -- 按阵营魔力加
    local classId = user.dbase:query("active_pet", 0)
    local campId = CampM.getPetCamp(classId)
    prop = combine(user, "camp_magic", campId);
    magic = apply(prop, magic);

    -- 按系别魔力加
    local departmentId = PetM.query(classId, "department_order")[1];
    prop = combine(user, "department_magic", departmentId);
    magic = apply(prop, magic);

    -- 贤者之语加成
    local stoneProp = ME.user.dbase:query("stone_prop", {});
    magic = magic + (stoneProp["magic"] or 0);

    -- 魔藤辅助
    local bine = user.bine;
    local bineAddon = user.dbase:query("bine_addon");
    if  type(bineAddon) == "table" and bine then
        magic = magic + (bineAddon["magic"] or 0);
    end

    -- 机械蜘蛛辅助
    local grids = SummonM.findSummonsByClassId(53);
    if #grids > 0 then
        local grid = grids[1];
        local summon = grid.summon;
        if summon and not summon:isDead() then
            prop = PropM.combine(summon, "attrib_to_user", 1);
            local summonMagic = summon:queryAttrib("magic") or 0;
            magic = magic + FormulaM.invoke("CALC_BIG_DIVISION", summonMagic, prop[3], 1000);
        end
    end

    -- 特定冈布奥魔力+
    local props = fetchProps(user, "slime_magic");
    for _, prop in ipairs(props) do
        if tonumber(prop[2]) == classId then
            magic = apply(prop, magic);
        end
    end

    -- 魔力+,特定冈布奥效果翻倍
    props = fetchProps(user, "slime_magic2");
    for _, prop in ipairs(props) do
        magic = apply(prop, magic);
        if tonumber(prop[2]) == classId then
            magic = apply(prop, magic);
        end
    end

    -- 魔力+,角斗场效果翻倍
    prop = combine(user, "wrestling_attrib", "magic");
    magic = apply(prop, magic);
    if ArenaM.isInArena() then
        magic = apply(prop, magic);
    end

    -- 角斗台属性增加
    if ArenaM.isInArena() then
        prop = combine(user, "attrib_arena", "magic");
        magic = apply(prop, magic);
    end

    -- --------------- 增益部分 ----------------------------------------------
    -- --------------- 主要包括状态提升，以及装备、技能的百分比提升 ----------
    -- --------------- 先计算总的加成，最后再相加（百分比计算时不叠加） ------
    addon = 0

    -- 百分比加成
    prop = combine(user, "attrib", "magic");
    addon = addon + apply(prop, magic) - magic;

    -- 只有一个敌人时
    prop = combine(user, "single_attrib", "magic");
    if prop[3] > 0 and 1 == #FormulaM.getAliveMonsters() then
        addon = addon + apply(prop, magic);
    end

    -- 每损失20%的生命值时魔力百分比提升（骑士精神）
    prop = combine(user, "knightship", "magic");
    if prop[3] > 0 then
        addon = addon + trigger(user, prop[1], prop[2], magic);
    end

    -- 状态增益的魔力
    prop = combine(user, "attrib3", "magic");
    addon = addon + apply(prop, magic);

    -- 静电场
    prop = combine(user, "attrib_elect", "magic");
    addon = addon + apply(prop, magic);

    -- 增益后的魔力
    magic = magic + addon;
    -- --------------- 减益部分 -------------------------------------------
    -- --------------- 包括各种减益 ---------------------------------------
    -- --------------- 先百分比后绝对值，百分比计算时直接叠加 -------------
    -- 被削弱
    if not args then
        -- 百分比削弱
        prop = combine(user, "reduce_attrib", "magic");
        magic = apply(prop, magic);

        -- 震慑状态（百分比-）
        prop = combine(user, "frighten", 1);
        magic = apply(prop, magic);

        -- 电磁场力状态（百分比-）
        prop = combine(user, "electricity", 1);
        if prop[3] > 0 then
            magic = magic - trigger(user, prop[1], prop[2], magic);
        end

        -- 绝对值削弱
        prop = combine(user, "reduce_attrib2", "magic");
        magic = apply(prop, magic);
    end

    -- 魔力最小为1
    if magic < 1 then
        magic = 1;
    end

    return magic;
end
